package com.vanysoft.business.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.sys.service.UserInfoService;
import com.vanysoft.common.utils.Excel.EasyPoiUtil;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 *
 * @author qi_ma
 */
@RequestMapping("/sys/user")
@Controller
public class UserController extends BaseController {
    @Resource
    private UserInfoService userInfoService;

    /**
     * 视图
     */
    @RequestMapping("user")
    public String user() {
        return "sys/user";
    }

    /**
     * 分页查询用户列表
     */
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse getRoleInfo(Page<UserInfo> page) throws VanySoftBaseException {
        page = userInfoService.selectAll(page);
        return new JSONResponse(page);
    }

    /**
     * 根据角色ID查询用户信息
     *
     * @param roleIds 角色ID数组
     *                url参数：
     *                roleIds = a     将解析为[a]
     *                roleIds = a,b,c 将解析为[a,b,c]
     *                不需要手动解析
     */
    @ResponseBody
    @RequestMapping("selectByRoleIds")
    public JSONResponse selectByRoleIds(String[] roleIds) {
        List<UserInfo> userInfos = userInfoService.selectByRoleIds(roleIds);
        return new JSONResponse(userInfos);
    }


    /**
     * 导出excel
     */
    @RequestMapping("export")
    public void exportExcel(@RequestParam Map<String, Object> condition) {
        List<UserInfo> userInfos = userInfoService.selectAll(condition);
        EasyPoiUtil.exportExcel(userInfos, "", "sheet01", UserInfo.class, "user_list.xlsx", getResponse());
    }

}
